import list from './components/list.vue'
export default {
  data() {
    return {
      showRecommend: false,
      inquiryOrderList: [
        {
          isFirstLoad: true,
          loadStatusId: 0,
          showimg: false,
          pageIndex: 1,
          list: [],
        },
        {
          isFirstLoad: true,
          loadStatusId: 0,
          pageIndex: 1,
          showimg: false,
          list: [],
        },
        {
          isFirstLoad: true,
          loadStatusId: 0,
          pageIndex: 1,
          showimg: false,
          list: [],
        },
        {
          isFirstLoad: true,
          loadStatusId: 0,
          pageIndex: 1,
          showimg: false,
          list: [],
        },
        {
          isFirstLoad: true,
          loadStatusId: 0,
          pageIndex: 1,
          showimg: false,
          list: [],
        },
      ],
      quoteOrderList: [
        {
          isFirstLoad: true,
          loadStatusId: 0,
          pageIndex: 1,
          showimg: false,
          list: [],
        },
        {
          isFirstLoad: true,
          loadStatusId: 0,
          pageIndex: 1,
          list: [],
        },
        {
          isFirstLoad: true,
          loadStatusId: 0,
          pageIndex: 1,
          list: [],
        },
        {
          isFirstLoad: true,
          loadStatusId: 0,
          pageIndex: 1,
          list: [],
        },
        {
          isFirstLoad: true,
          loadStatusId: 0,
          pageIndex: 1,
          list: [],
        },
      ],
      list: [
        {
          name: '询价订单',
        },
        {
          name: '报价订单',
        },
      ],
      inquiryStatusTitleList: [
        {
          staus: -1,
          name: '全部',
        },
        {
          staus: 0,
          name: '待付款',
        },
        {
          staus: 10,
          name: '待发货',
        },
        {
          staus: 20,
          name: '待收货',
        },
        {
          staus: 30,
          name: '已完成',
        },
      ],
      quoteStatusTitleList: [
        {
          staus: -1,
          name: '全部',
        },
        {
          staus: 0,
          name: '待付款',
        },
        {
          staus: 10,
          name: '待发货',
        },
        {
          staus: 20,
          name: '已发货',
        },
        {
          staus: 30,
          name: '已完成',
        },
      ],
      current: 0,
      swiperCurrent: 0,
      inquiryCurrent: 0, //询价
      quotationCurrent: 0, //报价
      showModal: false, //弹窗
      modalBtnLeft: '取消',
      modalBtnRight: '确定删除',
      modalTitle: '取消订单',
      dx: 0,
      loadStatus: ['loadmore', 'loading', 'nomore'],
      loadStatusId: 0,
      popup: {
        show: false,
        title: '',
        btnText: '',
        mode: 'middle',
      },
      goInPage: '', //msg为从聊天页面进入
      pageLoading: false, //初始化加载效果
    }
  },
  computed: {
    storeId() {
      return this.$store.state.userInfoDetail.store.storeId
    },
  },
  components: {
    list,
  },
  onLoad(e) {
    this.current = Number(e.tab)
    this.swiperCurrent = this.current
    this.goInPage = e.type ? e.type : ''
  },
  onShow() {
    this.initPage()
    this.getOrderList()
  },

  methods: {
    // 监听按钮
    handleOrder(val) {
      if (val.val === 'del') {
        this.popup.show = true
        this.popup.title = '请确认您是否删除此订单'
        this.popup.btnText = '确认删除'
        this.popup.val = val.val
        this.popup.id = val.id
      } else if (val.val === 'getGoods') {
        this.popup.show = true
        this.popup.title = '请确认您是否已收到完好货品？'
        this.popup.btnText = '确认收货'
        this.popup.val = val.val
        this.popup.id = val.id
      }
    },
    popupFn() {
      if (this.popup.val === 'del') {
        this.DeleteOrderTask(this.popup.id)
      } else if (this.popup.val === 'getGoods') {
        this.confirmReceive(this.popup.id)
      }
    },

    //确认收货
    confirmReceive(id) {
      this.$api.accessory
        .confirmReceive({
          id: id,
          store_id: this.storeId,
        })
        .then((res) => {
          this.popup.show = false
          if (res.code === 200) {
            uni.showToast({
              title: '收货成功',
              duration: 2000,
              icon: 'success',
            })
            setTimeout(() => {
              this.inquiryOrderList[0].isFirstLoad = true
              this.inquiryOrderList[0].loadStatusId = 0
              this.inquiryOrderList[0].pageIndex = 1
              this.inquiryOrderList[0].showimg = false

              this.inquiryOrderList[3].isFirstLoad = true
              this.inquiryOrderList[3].loadStatusId = 0
              this.inquiryOrderList[3].pageIndex = 1
              this.inquiryOrderList[3].showimg = false

              this.inquiryOrderList[4].isFirstLoad = true
              this.inquiryOrderList[4].loadStatusId = 0
              this.inquiryOrderList[4].pageIndex = 1
              this.inquiryOrderList[4].showimg = false
              this.getOrderList()
            }, 2000)
          }
        })
        .catch((e) => {
          console.log(e)
          this.popup.show = false
        })
    },

    // 删除订单
    async DeleteOrderTask(id) {
      const res = await this.$api.accessory.deleteOrder({
        id: id,
        store_id: this.storeId,
      })
      try {
        this.popup.show = false
        if (res.code === 200) {
          uni.showToast({
            title: '删除成功',
            duration: 2000,
            icon: 'success',
          })
          setTimeout(() => {
            if (this.current) {
              this.quoteOrderList[0].isFirstLoad = true
              this.quoteOrderList[0].loadStatusId = 0
              this.quoteOrderList[0].pageIndex = 1
              this.quoteOrderList[0].showimg = false

              this.quoteOrderList[4].isFirstLoad = true
              this.quoteOrderList[4].loadStatusId = 0
              this.quoteOrderList[4].pageIndex = 1
              this.quoteOrderList[4].showimg = false
            } else {
              this.inquiryOrderList[0].isFirstLoad = true
              this.inquiryOrderList[0].loadStatusId = 0
              this.inquiryOrderList[0].pageIndex = 1
              this.inquiryOrderList[0].showimg = false

              this.inquiryOrderList[4].isFirstLoad = true
              this.inquiryOrderList[4].loadStatusId = 0
              this.inquiryOrderList[4].pageIndex = 1
              this.inquiryOrderList[4].showimg = false
            }

            this.getOrderList()
          }, 2000)
        }
      } catch (e) {
        this.popup.show = false
        //TODO handle the exception
      }
    },

    // 订单状态点击事件
    sectionChange(index) {
      this.current === 0
        ? (this.inquiryCurrent = index)
        : (this.quotationCurrent = index)
      this.getOrderList()
    },

    // 页面数据 (当前tab，当前状态)
    getOrderList() {
      if (this.current) {
        if (this.quoteOrderList[this.quotationCurrent].isFirstLoad) {
          this.getQuoteList()
        }
      } else {
        if (this.inquiryOrderList[this.inquiryCurrent].isFirstLoad) {
          this.getInquiryList()
        }
      }
    },

    //获取我的询价订单
    async getInquiryList() {
      if (this.inquiryOrderList[this.inquiryCurrent].loadStatusId === 2) {
        return
      }
      if (this.pageLoading) {
        uni.showLoading({
          title: '加载中',
        })
      }
      this.inquiryOrderList[this.inquiryCurrent].loadStatusId = 1
      if (this.inquiryOrderList[this.inquiryCurrent].pageIndex === 1) {
        this.inquiryOrderList[this.inquiryCurrent].list = []
        this.inquiryOrderList[this.inquiryCurrent].showimg = false
      }
      const res = await this.$api.accessory.inquiryOrderList({
        pageIndex: this.inquiryOrderList[this.inquiryCurrent].pageIndex,
        pageSize: 10,
        status: this.inquiryStatusTitleList[this.inquiryCurrent].staus,
        storeid: this.storeId,
      })
      try {
        if (res.code === 200) {
          if (res.data.list.length) {
            this.inquiryOrderList[this.inquiryCurrent].loadStatusId = 0
            this.inquiryOrderList[
              this.inquiryCurrent
            ].list = this.inquiryOrderList[this.inquiryCurrent].list.concat(
              res.data.list
            )
            if (
              res.data.total >
              this.inquiryOrderList[this.inquiryCurrent].pageIndex * 10
            ) {
              this.inquiryOrderList[this.inquiryCurrent].loadStatusId = 0
            } else {
              this.inquiryOrderList[this.inquiryCurrent].loadStatusId = 2
            }
          } else {
            this.inquiryOrderList[this.inquiryCurrent].showimg = true
            this.inquiryOrderList[this.inquiryCurrent].loadStatusId = 2
          }
        }
        this.inquiryOrderList[this.inquiryCurrent].pageIndex++
        this.inquiryOrderList[this.inquiryCurrent].isFirstLoad = false
      } catch (err) {
        console.log(err)
      }
      if (this.pageLoading) {
        uni.hideLoading()
      } else {
        this.pageLoading = true
      }
    },

    //获取我的报价订单
    async getQuoteList() {
      if (this.quoteOrderList[this.quotationCurrent].loadStatusId === 2) {
        return
      }
      if (this.pageLoading) {
        uni.showLoading({
          title: '加载中',
        })
      }
      this.quoteOrderList[this.quotationCurrent].loadStatusId = 1
      if (this.quoteOrderList[this.quotationCurrent].pageIndex === 1) {
        this.quoteOrderList[this.quotationCurrent].list = []
        this.quoteOrderList[this.quotationCurrent].showimg = false
      }
      const res = await this.$api.accessory.quoteOrderList({
        pageIndex: this.quoteOrderList[this.quotationCurrent].pageIndex,
        pageSize: 10,
        status: this.quoteStatusTitleList[this.quotationCurrent].staus,
        storeid: this.storeId,
      })

      try {
        if (res.code === 200) {
          if (res.data.list.length) {
            this.quoteOrderList[this.quotationCurrent].loadStatusId = 0
            this.quoteOrderList[
              this.quotationCurrent
            ].list = this.quoteOrderList[this.quotationCurrent].list.concat(
              res.data.list
            )
            if (
              res.data.total >
              this.quoteOrderList[this.quotationCurrent].pageIndex * 10
            ) {
              this.quoteOrderList[this.quotationCurrent].loadStatusId = 0
            } else {
              this.quoteOrderList[this.quotationCurrent].loadStatusId = 2
            }
          } else {
            this.quoteOrderList[this.quotationCurrent].showimg = true
            this.quoteOrderList[this.quotationCurrent].loadStatusId = 2
          }
        }
        this.quoteOrderList[this.quotationCurrent].pageIndex++
        this.quoteOrderList[this.quotationCurrent].isFirstLoad = false
      } catch (err) {
        console.log(err)
      }
      if (this.pageLoading) {
        uni.hideLoading()
      } else {
        this.pageLoading = true
      }
    },

    // tab栏切换
    change(index) {
      this.current = index
      this.swiperCurrent = index
    },

    // swiper 改变
    orderListchange(e) {
      this.swiperCurrent = e.detail.current
      this.current = e.detail.current
      this.getOrderList()
    },

    // swiper 切换事件
    transition({ detail: { dx } }) {
      this.$refs.tabs.setDx(dx)
    },

    animationfinish({ detail: { current } }) {
      this.$refs.tabs.setFinishCurrent(current)
      this.swiperCurrent = current
      this.current = current
    },

    //重置页面数据
    initPage() {
      for (let item in this.inquiryOrderList) {
        this.inquiryOrderList[item].isFirstLoad = true
        this.inquiryOrderList[item].loadStatusId = 0
        this.inquiryOrderList[item].showimg = false
        this.inquiryOrderList[item].pageIndex = 1
        this.inquiryOrderList[item].list = []
      }
      for (let item in this.quoteOrderList) {
        this.quoteOrderList[item].isFirstLoad = true
        this.quoteOrderList[item].loadStatusId = 0
        this.quoteOrderList[item].showimg = false
        this.quoteOrderList[item].pageIndex = 1
        this.quoteOrderList[item].list = []
      }
    },
  },
}
